﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Simple.Entity;
using Simple.Common.Data;
using Simple.Common.Pager;
using Simple.Common.Core.Pager;
using Simple.Common.EasyUICommon;
using Simple.ViewModel;
using Simple.Entity.Models;

namespace Simple.Service
{
    public class DataDictionaryService : ApplicationService
    {
        private readonly WorkEfRepository<Sys_DataDictionary> _dataDictionaryRepository;

        public DataDictionaryService(WorkEfRepository<Sys_DataDictionary> dataDictionaryRepository)
        {
            _dataDictionaryRepository = dataDictionaryRepository;
        }

        public Sys_DataDictionary FindById(long id)
        {
            return _dataDictionaryRepository.Find(id);
        }

        public IPagedList<DataDictionaryListModel> GetPageList(SimpleMvcPage page, DataDictionaryQueryModel queryModel)
        {
            var query = _dataDictionaryRepository.TableNoTracking.Select(a => new DataDictionaryListModel
            {
                Id = a.Id,
                ParentId = a.ParentId,
                Sort = a.Sort,
                CreateTime = a.CreateTime,
                Code = a.Code,
                Title = a.Title
            });

            query = query.Where(m => m.ParentId > 0);
            query = query.OrderBy(m => m.ParentId).ThenBy(m => m.Sort);
            var model = query.ToPagedList(page);
            return model;
        }

        public IPagedList<DataDictionaryListModel> GetParentPageList(SimpleMvcPage page, DataDictionaryQueryModel queryModel)
        {
            var query = _dataDictionaryRepository.TableNoTracking.Select(a => new DataDictionaryListModel
            {
                Id = a.Id,
                ParentId = a.ParentId,
                Sort = a.Sort,
                CreateTime = a.CreateTime,
                Code = a.Code,
                Title = a.Title
            });
            query = query.Where(m => m.ParentId == 0);
            query = query.OrderBy(m => m.ParentId).ThenBy(m => m.Sort);
            var model = query.ToPagedList(page);
            return model;
        }

        public SimpleResult AddParent(Sys_DataDictionary Sys_DataDictionary)
        {
            int count = _dataDictionaryRepository.TableNoTracking.Count(m => m.ParentId == Sys_DataDictionary.ParentId && m.Title == Sys_DataDictionary.Title);
            if (count > 0)
            {
                return GetError("已经存在相同的字典分类");
            }

            int ret = _dataDictionaryRepository.Add(Sys_DataDictionary);
            return GetResult(ret);
        }

        public SimpleResult UpdateParent(Sys_DataDictionary Sys_DataDictionary)
        {
            return Update(Sys_DataDictionary);
        }

        public SimpleResult Add(Sys_DataDictionary Sys_DataDictionary)
        {
            int count = _dataDictionaryRepository.TableNoTracking.Count(m => m.ParentId == Sys_DataDictionary.ParentId && (m.Title == Sys_DataDictionary.Title || m.Code == Sys_DataDictionary.Code));
            if (count > 0)
            {
                return GetError("已经存在相同的字典");
            }

            int ret = _dataDictionaryRepository.Add(Sys_DataDictionary);
            return GetResult(ret);
        }

        public SimpleResult Update(Sys_DataDictionary Sys_DataDictionary)
        {
            int ret = _dataDictionaryRepository.Update(Sys_DataDictionary);
            return GetResult(ret);
        }

        public SimpleResult Delete(long id)
        {
            var delDictionary = _dataDictionaryRepository.Find(id);
            if (delDictionary == null)
            {
                return GetError();
            }
            int ret = _dataDictionaryRepository.Delete(delDictionary);
            return GetResult(ret);
        }


    }
}
